package com.wzdigit.wms.basic.service.code;

import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.inventory.GoodsVersion;
import com.wzdigit.wms.basic.domain.inventory.SimpleGoodsVersion;
import com.wzdigit.wms.basic.mapper.core.inventory.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class GoodsVersionService {

    @Autowired
    private GoodsVersionMapper goodsVersionMapper;
    
    /**
     * SN查询
     * 
     * @param sn
     * @return
     */
    public GoodsVersion selectBySn(String sn) {
        return goodsVersionMapper.selectBySn(sn);
    }
    
    /**
     * 主键查询
     * 
     * @param goodsId
     * @return
     */
    public GoodsVersion selectById(Integer goodsId) {
        return goodsVersionMapper.selectById(goodsId);
    }
    
    
    
    /**
     * SN集合查询
     * 
     * @param snList
     * @return
     */
    public List<GoodsVersion> selectBySnList(List<String> snList) {
        return goodsVersionMapper.selectBySnList(snList);
    }
    
    /**
     * 主键集合查询
     * 
     * @param goodsIdList
     * @return
     */
    public List<GoodsVersion> selectByIdList(List<Integer> goodsIdList) {
        return goodsVersionMapper.selectByIdList(goodsIdList);
    }
    
    
    
    /**
     * SN集合查询，简化
     * 
     * @param snList
     * @return
     */
    public List<SimpleGoodsVersion> selectSimpleBySnList(List<String> snList) {
        return goodsVersionMapper.selectSimpleBySnList(snList);
    }
    
    /**
     * 主键集合查询，简化
     * 
     * @param goodsIdList
     * @return
     */
    public List<SimpleGoodsVersion> selectSimpleByIdList(List<Integer> goodsIdList) {
        return goodsVersionMapper.selectSimpleByIdList(goodsIdList);
    }
    
    

    /**
     * 更新【实体】
     * 
     * 1、更新后的 [VERSION] 会返回给实体
     * 
     * @param goods
     */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void updateById(GoodsVersion goods) {
		Integer row = goodsVersionMapper.updateById(goods);
		if (row == 0) {
			log.info(String.format("条码[%s]属性发生变更，版本号[%s]，更新失败", goods.getGoodsSn(), goods.getVersion()));
			Shift.fatal(String.format("条码[%s]属性发生变更，稍后请重试！", goods.getGoodsSn()));
		}
	}
    
    /**
     * 更新【数量】
     * 
     * 1、入参 [QTY] =变更的阀值
     * 1、更新后的 [QTY] 会返回给实体
     * 2、更新后的 [VERSION] 会返回给实体
     * 
     * @param goods
     */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void updateQtyById(GoodsVersion goods) {
		Integer row = goodsVersionMapper.updateQtyById(goods);
		if (row == 1) {
			row = goodsVersionMapper.updateById(goods);
		}
		if (row == 0) {
			log.info(String.format("条码[%s]属性发生变更，版本号[%s]，更新失败", goods.getGoodsSn(), goods.getVersion()));
			Shift.fatal(String.format("条码[%s]属性发生变更，稍后请重试！", goods.getGoodsSn()));
		}
	}
}